
Partial Class usercontrols_UCImageFader
    Inherits System.Web.UI.UserControl

    Public SlideShowSpeedMs As Integer = 3000
    Public FaderSpeedMs As Integer = 250

    Public WriteOnly Property ImageUrl() As String
        Set(ByVal Value As String)
            Image1.ImageUrl = Value
        End Set
    End Property

    Public Class CImageLinkTooltip
        Public Image As String
        Public Link As String
        Public Tooltip As String
    End Class

    Public Sub Display(captionsAndImages As Dictionary(Of String, String()))
        If IsNothing(captionsAndImages) Then
            Me.Visible = False
            Exit Sub
        End If

        Dim dir As String = CConfig.DropboxPath & "/photos/"
        Dim list As New List(Of CImageLinkTooltip)

        Dim count As Integer = 0
        Dim total As Integer = 0
        For Each i As String In captionsAndImages.Keys
            total += captionsAndImages(i).Length
        Next

        For Each i As String In captionsAndImages.Keys
            For Each j As String In captionsAndImages(i)
                count += 1
                Dim d As New CImageLinkTooltip
                d.Image = "http://mikeconnellministries.com/" & j.Substring(dir.Length).Replace("\", "/")
                d.Link = "http://duncanbrownphotography.co.nz"
                d.Tooltip = String.Concat(count, " of ", total, ": ", i)
                list.Add(d)
            Next
        Next
        Display(list)
    End Sub
    Public Sub Display(data As List(Of CImageLinkTooltip))
        'If image is set, skip
        If Len(Image1.ImageUrl) > 0 Then Exit Sub
        Image1.Visible = False

        'Exit early
        If data.Count = 0 Then
            Me.Visible = False
            Exit Sub
        End If

        'Set up script
        Dim sb As New StringBuilder()
        sb.Append("var f = new Fader('").Append(Image1.ClientID).AppendLine("');").AppendLine("FADERS.push(f);")
        sb.Append("f.rotateSpeed = ").Append(SlideShowSpeedMs).AppendLine(";")
        sb.Append("f.fadeSpeed = ").Append(FaderSpeedMs / 1000.0).AppendLine(";")
        For Each i In data
            If Len(Trim(i.Tooltip)) > 0 Then
                sb.Append("f.Add('").Append(i.Image).Append("','").Append(i.Link).Append("', '").Append(i.Tooltip).AppendLine("');")
            End If
            If Len(Trim(i.Link)) > 0 Then
                Image1.Style.Add("cursor", "hand")
            End If
        Next
        litScript.Text = sb.ToString

        Image1.Attributes.Add("onclick", "OnImageClick('" & Image1.ClientID & "')")

        With Image1
            .Visible = True
            If data.Count > 0 Then
                .ImageUrl = data(0).Image
                .AlternateText = data(0).Tooltip
                .ToolTip = .AlternateText
            End If
        End With
    End Sub
End Class
